package com.epam.edu.dao;


import com.epam.edu.entity.OrdersMarks;
import com.epam.edu.entity.Subject;

import java.sql.*;

public class DAOOrdersMarks {
    public static int add(Connection connection, OrdersMarks ordersMarks) {
        int id = -1;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "INSERT INTO Marks (orderId,historyKZ,language,mathematics,subjId,mark) " +
                    "VALUES(?,?,?,?,?,?)";
            statement = connection.prepareStatement(command, Statement.RETURN_GENERATED_KEYS);
            statement.setInt(1, ordersMarks.getOrderId());
            statement.setInt(2, ordersMarks.getHistoryKZMark());
            statement.setInt(3, ordersMarks.getLanguageMark());
            statement.setInt(4, ordersMarks.getMathematicsMark());
            statement.setInt(5, ordersMarks.getSubject().getId());
            statement.setInt(6, ordersMarks.getMark());
            statement.executeUpdate();
            resultSet = statement.getGeneratedKeys();
            resultSet.next();
            id = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return id;
    }

    public static OrdersMarks selectByOrderId(Connection connection, int orderId) {
        OrdersMarks ordersMarks = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String command = "SELECT id,orderId,historyKZ,language,mathematics,subjId,mark " +
                    "FROM Marks WHERE orderId=? AND isDelete='false'";
            statement = connection.prepareStatement(command);
            statement.setInt(1, orderId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                ordersMarks = new OrdersMarks(orderId);
                ordersMarks.setId(resultSet.getInt(1));
                ordersMarks.setHistoryKZMark(resultSet.getInt(3));
                ordersMarks.setLanguageMark(resultSet.getInt(4));
                ordersMarks.setMathematicsMark(resultSet.getInt(5));
                Subject subject = DAOSubject.selectById(connection, resultSet.getInt(6));
                ordersMarks.setSubject(subject);
                ordersMarks.setMark(resultSet.getInt(7));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (resultSet != null) resultSet.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        return ordersMarks;
    }
}

